/**
 * 
 */
package org.jenkon.commons.lang.builder;

import java.io.Serializable;

/**
 * @author Jens Konopka
 * 
 */
public class HashCodeBuilder implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6448990913326728964L;

	private static final int DEFAULT_PRIME = 31;

	private int prime;
	private int result = 1;

	/**
	 * initialize constructor. set the {@link #prime} to {@value #DEFAULT_PRIME}.
	 */
	public HashCodeBuilder() {
		this(DEFAULT_PRIME);
	}

	/**
	 * initialize constructor.
	 * 
	 * @param prime set the {@link #prime}.
	 */
	public HashCodeBuilder(final int prime) {
		this.prime = prime;
	}

	/**
	 * @param value the value to append to the currently hashcode
	 * @return {@link HashCodeBuilder}
	 */
	public HashCodeBuilder append(final Object value) {
		calc(value);
		return this;
	}

	/**
	 * @param value the value to append hashcode to the currently hashcode
	 */
	private void calc(final Object value) {
		result = prime * result + ((value == null) ? 0 : value.hashCode());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		final StringBuilder builder = new StringBuilder();
		builder.append("HashCodeBuilder [prime=");
		builder.append(prime);
		builder.append(", result=");
		builder.append(result);
		builder.append("]");
		return builder.toString();
	}

}
